﻿using iTextSharp.text;
using iTextSharp.text.pdf;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ItextSharpDemo
{
    public class TablePdf : IPdfCreator
    {
        public void Create(string fileName)
        {
            var doc = new Document();
            PdfWriter.GetInstance(doc, new FileStream(fileName, FileMode.Create));


            doc.Open();

            doc.Add(CreateTable());

            doc.Close();
        }

        PdfPTable CreateTable()
        {
            PdfPTable table = new PdfPTable(4);
            table.TotalWidth = 400f;
            table.LockedWidth = true;
            table.HeaderRows = 1;

            PdfPCell header = new PdfPCell(new Phrase("Header"));
            header.BorderWidth = 0;
            header.HorizontalAlignment = PdfCell.ALIGN_CENTER;
            header.Colspan = 4;
            

            table.AddCell(header);

            for (int i = 0; i < 100; i++)
            {
                table.AddCell("Cell 1");
                table.AddCell("Cell 2");
                table.AddCell("Cell 3");
                table.AddCell("Cell 4");
            }

            PdfPTable nested = new PdfPTable(1);
            nested.AddCell("Nested Row 1");
            nested.AddCell("Nested Row 2");
            nested.AddCell("Nested Row 3");

            PdfPCell nesthousing = new PdfPCell(nested);
            nesthousing.Padding = 0f;

            table.AddCell(nesthousing);

            PdfPCell bottom = new PdfPCell(new Phrase("bottom"));
            bottom.Colspan = 3;
            bottom.HorizontalAlignment = PdfCell.ALIGN_CENTER;
            bottom.VerticalAlignment = PdfCell.ALIGN_CENTER;

            table.AddCell(bottom);

            return table;
        }
    }
}
